<?php
$link = mysql_connect('localhost','cs4350','!@'); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysql_error()); 
}
else {
    echo 'You are connected to the cs4350 database server!</br>';
}
if (!mysql_select_db( 'todo', $link)) {
    die('Aw snap, no database named todo...');
}
else {
    echo 'You are now using the todo database!</br>';
}
$username = $_POST["nUser"];
$password = md5($_POST["nPass"]);


$sql = "INSERT INTO users VALUES ('$_POST[nUser]','$password','$_POST[email]','$_POST[fName]','$_POST[lName]')";
//die($sql);

$test = 'SELECT * FROM users';

$result = mysql_query($sql, $link);
$testResult = mysql_query($test, $link);

if (!$result) {
	header('location:invalid.html#toReg');
	//die($sql);
//    die('Invalid query: ' . mysql_error());
}
else {
    // print_r($result);
    // var_dump($result);
    
	//echo 'User added </br>';
	echo '<head>';
	echo '<script>';
	echo 'window.onload = function(){';
	echo 'sessionStorage.setItem(\'user\', "' . $username . '");';
	echo 'var login = sessionStorage.getItem("user");';
	//echo 'alert(login);';
	echo 'window.location = "/home/home.php?user='.$username . '";';
	echo '}';
	echo '</script>';
	echo '</head>';
	
    mysql_free_result($result);
	mysql_free_result($testResult);
}
?>